package com.ews.onlineexchange.controller;

import java.io.Serializable;
import java.math.BigDecimal;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;

@Named
@Stateless
public class StatisticsController implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;


	@Inject
	EntityManager entityManager;
	
	private final static String GET_SUPPLIER_COUNT = ""
			+ " SELECT COUNT(supplier) "
			+ " FROM ServiceSupplier supplier "
			+ " WHERE supplier.delFlag = '0' ";
	
	public long getSupplierCount() {
		return (long)entityManager.createQuery(GET_SUPPLIER_COUNT).getSingleResult();
	}
	
	private final static String GET_BUYER_COUNT = ""
			+ " SELECT COUNT(buyer) "
			+ " FROM ServiceBuyer buyer "
			+ " WHERE buyer.delFlag = '0' ";
	
	public long getBuyerCount() {
		return (long)entityManager.createQuery(GET_BUYER_COUNT).getSingleResult();
	}
	
	/**
	 * 当天发布
	 */
	private final static String GE_AUDITEDT_BULLETIN_COUNT_TODAY = ""
			+ " SELECT COUNT(*)  " 
			+ " FROM bulletin b "
			+ "  WHERE to_char(sysdate,'yyyy-MM-dd') = to_char(b.audittime,'yyyy-MM-dd')" 
			+ " AND b.DELFLAG = '0' ";
	
	public BigDecimal getAuditedBulletinTodayCount() {
		return (BigDecimal)entityManager.createNativeQuery(GE_AUDITEDT_BULLETIN_COUNT_TODAY).getSingleResult();
	}
	
	/**
	 * 当天中选
	 */
	private final static String GET_OVER_BULLETIN_COUNT_TODAY = ""
			+ " SELECT COUNT(*)  " 
			+ " FROM bulletin b "
			+ " WHERE b.OVER = 1 " 
			+ " AND to_char(sysdate,'yyyy-MM-dd') = to_char(b.overtime,'yyyy-MM-dd') "
			+ " AND b.DELFLAG = '0' ";
	
	public BigDecimal getOverBulletinTodayCount() {
		return (BigDecimal)entityManager.createNativeQuery(GET_OVER_BULLETIN_COUNT_TODAY).getSingleResult();
	}
	
	/**
	 * 当月中选
	 */
	private final static String GET_OVER_BULLETIN_COUNT_MONTH = ""
			+ " SELECT COUNT(*)  " 
			+ " FROM bulletin b "
			+ " WHERE b.OVER = 1 " 
			+ " AND to_char(sysdate,'yyyy-MM') = to_char(b.overtime,'yyyy-MM') "
			+ " AND b.DELFLAG = '0' ";
	
	public BigDecimal getOverBulletinMonthCount() {
		return (BigDecimal)entityManager.createNativeQuery(GET_OVER_BULLETIN_COUNT_MONTH).getSingleResult();
	}
	
	/**
	 * 当月中选
	 */
	private final static String GET_OVER_BULLETIN_COUNT_ALL = ""
			+ " SELECT COUNT(*)  " 
			+ " FROM bulletin b "
			+ " WHERE b.OVER = 1 " 
			+ " AND b.DELFLAG = '0' ";
	
	public BigDecimal getOverBulletinAllCount() {
		return (BigDecimal)entityManager.createNativeQuery(GET_OVER_BULLETIN_COUNT_ALL).getSingleResult();
	}
	
}
